
package com.epam.android.training.day10.task2;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Activity that controls demo application settings saving and loadig.
 * 
 * @author Anton Novikau
 */
public class MainActivity extends Activity {
  private static final String PREFS_NAME = "SharedPrefsAppPreferences";

  private static final String FIRST_NAME_KEY = "first_name";

  private static final String LAST_NAME_KEY = "last_name";

  private static final String AGE_KEY = "age";

  private EditText firstNameView;

  private EditText lastNameView;

  private EditText ageView;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // retrieve edit fields from view.
    firstNameView = (EditText) findViewById(R.id.first_name);
    lastNameView = (EditText) findViewById(R.id.last_name);
    ageView = (EditText) findViewById(R.id.age);

    // load preferences and populate edit fields with the data from preferences.
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    String firstName = settings.getString(FIRST_NAME_KEY, null);
    String lastName = settings.getString(LAST_NAME_KEY, null);
    int age = settings.getInt(AGE_KEY, 0);

    firstNameView.setText(firstName);
    lastNameView.setText(lastName);
    ageView.setText(Integer.toString(age));

    // register shared preferences listener.
    registerListener(settings);

    // add action to save button.
    addSaveAction();
  }

  /**
   * Adds shared preferences listener to specified {@link SharedPreferences}
   * instance.
   * 
   * @param settings {@link SharedPreferences} instance to add listener
   */
  private void registerListener(SharedPreferences settings) {
    settings
        .registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() {

          @Override
          public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            Toast.makeText(MainActivity.this, getString(R.string.prefs_listener_message, key),
                Toast.LENGTH_SHORT).show();
          }
        });
  }

  /**
   * Adds action to save button which must persists form data to shared
   * preferences.
   */
  private void addSaveAction() {
    Button saveButton = (Button) findViewById(R.id.submit_prefs);
    saveButton.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        String firstName = firstNameView.getText().toString();
        String lastName = lastNameView.getText().toString();
        int age = Integer.parseInt(ageView.getText().toString());

        SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);

        SharedPreferences.Editor settingsEditor = settings.edit();

        settingsEditor.putString(FIRST_NAME_KEY, firstName);
        settingsEditor.putString(LAST_NAME_KEY, lastName);
        settingsEditor.putInt(AGE_KEY, age);

        settingsEditor.commit();
        Toast.makeText(MainActivity.this, R.string.prefs_saved_message, Toast.LENGTH_SHORT).show();
      }
    });
  }
}
